package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljEnterpriseVo;
import org.dromara.xhlj.domain.query.XhljEnterpriseQuery;
import org.dromara.xhlj.domain.bo.XhljEnterpriseBo;
import org.dromara.xhlj.crud.service.IXhljEnterpriseCrudService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-企业展商
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/enterprise")
public class XhljEnterpriseCrudController extends BaseController {

    private final IXhljEnterpriseCrudService xhljEnterpriseService;

    /**
     * 分页查询企业展商列表
     */
    @SaCheckPermission("xhlj:enterprise:list")
    @GetMapping("/list")
    public TableDataInfo<XhljEnterpriseVo> list(XhljEnterpriseQuery query, PageQuery pageQuery) {
         return xhljEnterpriseService.queryPageList(query, pageQuery);
    }

    /**
     * 查询企业展商列表
     */
    @SaCheckPermission("xhlj:enterprise:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljEnterpriseVo> allList(XhljEnterpriseQuery query) {
        List<XhljEnterpriseVo> list =  xhljEnterpriseService.queryList(query);
        return TableDataInfo.build(list);
    }


    /**
     * 导出企业展商列表
     */
    @SaCheckPermission("xhlj:enterprise:export")
    @Log(title = "企业展商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljEnterpriseQuery query, HttpServletResponse response) {
        List<XhljEnterpriseVo> list = xhljEnterpriseService.queryList(query);
        ExcelUtil.exportExcel(list, "企业展商", XhljEnterpriseVo.class, response);
    }

    /**
     * 获取企业展商详细信息
     *
     * @param enterpriseId 主键
     */
    @SaCheckPermission("xhlj:enterprise:query")
    @GetMapping("/{enterpriseId}")
    public R<XhljEnterpriseVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer enterpriseId) {
        return R.ok(xhljEnterpriseService.queryById(enterpriseId));
    }

    /**
     * 新增企业展商
     */
    @SaCheckPermission("xhlj:enterprise:add")
    @Log(title = "企业展商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljEnterpriseBo bo) {
        bo.setCreateBy(Math.toIntExact(LoginHelper.getUserId()));
        return toAjax(xhljEnterpriseService.insertByBo(bo));
    }

    /**
     * 修改企业展商
     */
    @SaCheckPermission("xhlj:enterprise:edit")
    @Log(title = "企业展商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljEnterpriseBo bo) {
        return toAjax(xhljEnterpriseService.updateByBo(bo));
    }

    /**
     * 删除企业展商
     *
     * @param enterpriseIds 主键串
     */
    @SaCheckPermission("xhlj:enterprise:remove")
    @Log(title = "企业展商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] enterpriseIds) {
        return toAjax(xhljEnterpriseService.deleteWithValidByIds(List.of(enterpriseIds), true));
    }
}
